package com.myexam.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myexam.dao.StudentDao;
import com.myexam.model.Score;
import com.myexam.model.Student;
import com.myexam.service.StudentService;
import com.myexam.util.Page;
import com.myexam.util.PageResult;
import com.myexam.util.PageUtil;
@Service("studentService")
public class StudentServiceimpl implements StudentService {
	@Autowired
	private StudentDao studentDao;

	
	public boolean saveStudent(Student student){
		String studentID = student.getStudentID();
		if(studentDao.findByStudentID(studentID) == null){
			studentDao.addStudent(student);
			return true;
		}
		return false;
	}
	
	@Override
	public boolean allowLoagin(String studentID, String password) {
		// TODO Auto-generated method stub
		Student student = studentDao.findByStudentID(studentID);
		if(student == null){
			return false;
		}else{
			if(!password.equals(student.getPassword())){
				return false;
			}
		}
		return true;
	}

	@Override
	public Student getStudentInfo(String studentID) {
		// TODO Auto-generated method stub
		
		return studentDao.findByStudentID(studentID);
	}

	@Override
	public void setStudentResult(String studentID, int result,int examID) throws ParseException {
		// TODO Auto-generated method stub
		Score score = new Score();
		score.setExamID(examID);
		score.setStudentID(Integer.parseInt(studentID));
		score.setScore(result);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = format.format(new Date());
		score.setTime(time);
		studentDao.addScore(score);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public PageResult queryStudentByPage(Page page) {
		// TODO Auto-generated method stub
		page = PageUtil.createPage(page.getEveryPage(), studentDao.findStudentCount(), page.getCurrentPage());
		List<Student> list = studentDao.findStudentByPage(page);
		PageResult pageResult = new PageResult(page, list);
		return pageResult;
	}

	/*public PageResult likeQueryByStudentName(Page page){
		return null;
	}*/

}
